var urls = "http://localhost:8888/yihuan/";

/*全国省份*/
function DoctorProvince() {
    $.ajax({
        url: "findByProvince",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {

            } else {
                var str = null;
                for (var i = 0; i < data.ProvinceList.length; i++) {
                    str += "<option value='" + data.ProvinceList[i].provinceID + "'>" + data.ProvinceList[i].provinceName + "</option>";
                }
            }
            $("#Province").append(str);
        }
    });
}

/*全国县区*/
function changeCity() {
    //城市编号
    var cityID = $("#City").val();
    var DataSources = {
        "cityID": cityID
    };
    $.ajax({
        url: "findByCountry",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 500) {
                window.location.href = urls + "404-error.html";
            } else {
                var str = null;
                for (var i = 0; i < data.CountryList.length; i++) {
                    str += "<option value='" + data.CountryList[i].countryID + "'>" + data.CountryList[i].countryName + "</option>";
                }
            }
            $("#Country").empty();
            $("#Country").append(str);
        }
    });
}

/*全国城市*/
function changeProvince() {
    //省份编号
    var provinceID = $("#Province").val();
    var DataSources = {
        "provinceID": provinceID
    };
    if (provinceID == "0") {
        $("#City").empty();
        $("#Country").empty();
        $("#City").append("<option value='0'>=====请选择城市=====</option>");
        $("#Country").append("<option value='0'>=====请选择县区=====</option>");
    } else {
        $.ajax({
            url: "findByCity",
            type: "POST",
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(DataSources),
            success: function (data) {
                if (data.code == 500) {
                    window.location.href = urls + "404-error.html";
                } else {
                    var str = null;
                    for (var i = 0; i < data.CityList.length; i++) {
                        str += "<option value='" + data.CityList[i].cityID + "'>" + data.CityList[i].cityName + "</option>";
                    }
                }
                $("#City").empty();
                $("#City").append(str);
                changeCity();
            }
        });
    }
}

$("#compName").blur(function () {
    var compName = $("#compName").val();  //用户账号
    var data = {
        "compName": compName
    };
    $.ajax({
        url: urls + "findCompanyByCompName",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        async: false,
        success: function (data) {
            if (data.code == 200) {

            } else {
                Swal.fire("提示", "请重新输入账号", "error");
            }

        }
    });
});

//登记医院信息
function companyAddForm() {
    var formData = new FormData($("#companyAddForm")[0]);
    //公司名称
    var compName = $("#compName").val();
    //省份
    var Province = $("#Province option:selected").text();
    //城市
    var City = $("#City option:selected").text();
    //县区
    var Country = $("#Country option:selected").text();
    //详细地址
    var address = $("#compAddress").val();
    //县区编号
    var vaccinumCountryID = $("#Country option:selected").val();
    //个人地址
    var compAddress = Province + "省" + City + Country + address;
    //医院电话
    var compPhone = $("#compPhone").val();
    //医院简介
    var compProfile = $("#compProfile").val();
    //官方邮件
    var compEmail = $("#compEmail").val();
    /*存储数据*/
    formData.append("CountryID", vaccinumCountryID);
    formData.append("compName_1", compName);
    formData.append("compAddress_1", compAddress);
    formData.append("compPhone_1", compPhone);
    formData.append("compProfile_1", compProfile);
    formData.append("compEmail_1", compEmail);

    swal.fire({
        type: 'warning', // 弹框类型
        title: '确认', //标题
        text: "确认是否登录", //显示内容
        confirmButtonColor: '#3085d6',// 确定按钮的 颜色
        confirmButtonText: '确定',// 确定按钮的 文字
        showCancelButton: true, // 是否显示取消按钮
        cancelButtonColor: '#d33', // 取消按钮的 颜色
        cancelButtonText: "取消", // 取消按钮的 文字
        focusCancel: true, // 是否聚焦 取消按钮
        reverseButtons: true  // 是否 反转 两个按钮的位置 默认是  左边 确定  右边 取消
    }).then(function (isConfirm) {
        if (isConfirm.value) {
            //发送数据
            $.ajax({
                url: "insertCompanyByCompanyData",
                type: 'POST',
                data: formData,
                cache: false,
                async: false,
                processData: false,  //必须false才会避开jQuery对 formdata 的默认处理
                contentType: false,  //必须false才会自动加上正确的Content-Type
                success: function (data) {
                    if (data.code == 200) {
                        // Swal.fire("提示", "数据登记成功!", "success");
                        $.getJSON("hcontroller/addmiddletable", {uid: uid, hid: hid}, function (json) {
                            console.log(json);
                        });
                        window.open(urls + "Background-BorderCompany.html");
                        window.open("", "_self").close();
                        window.open("about:blank", "_top").close()
                    } else if (data.code == 300) {
                        Swal.fire("提示", "请提交相应材料!", "warning");
                    } else {
                        Swal.fire("提示", "登记数据有误!", "error");
                    }
                },
                error: function (data) {
                    if (data.code == 500) {
                        Swal.fire("提示", "数据提交失败!", "error");
                    }
                }
            });
        } else {
            Swal.fire("提示", "已取消提交", "error");
        }
    })
}

/*加载数据*/
$(function () {
    DoctorProvince();
});